package com.bds.tjj.action;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bds.tjj.model.ConfidenData;
import com.bds.tjj.model.MonthData;
import com.bds.tjj.model.PropIndex;
import com.bds.tjj.model.common.Result;
import com.bds.tjj.service.ConfidenSumService;
import com.bds.tjj.service.MonthSumService;
import com.bds.tjj.utils.Const;

/**
 * Created by guoyu on 2018/1/29.
 */
@Controller
public class MonthDetailController extends BaseAction {
	private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger
			.getLogger(MonthDetailController.class);

	@Autowired
	MonthSumService monthSumService;
	@Autowired
	ConfidenSumService confidenSumService;

	@RequestMapping(value = "/data/month/detail/index/{tt}", method = RequestMethod.GET)
	public String getMonthNum(Map<String, Object> map, @PathVariable String tt) throws IOException {

		if (StringUtils.isEmpty(tt)) {
			map.put("map", "");
		} else {
			map.put("map", tt);
		}

		return "ftl/analytics/month_detail_data";
	}

	@RequestMapping(value = "/data/month/detail/get/{tt}/{start}/{end}", method = RequestMethod.GET)
	@ResponseBody
	public Result getMonthSum(HttpServletRequest httpRequest, HttpServletResponse httpResponse, @PathVariable String tt,
			@PathVariable String start, @PathVariable String end) throws Exception {
		Result ret = new Result();
		Integer start_month = 0;
		Integer end_month = 0;
		try {
			start_month = Integer.parseInt(start);
		} catch (Exception e) {
			ret.setResult(Const.FAIL, "开始月份格式不对");
			return ret;
		}
		try {
			end_month = Integer.parseInt(end);
		} catch (Exception e) {
			ret.setResult(Const.FAIL, "结束月份格式不对");
			return ret;
		}
		try {
			PropIndex prop = monthSumService.getPropIndex(tt);

			if (prop != null) {
				if (prop.getChart_type().equals("conf")) {
					List<ConfidenData> list = confidenSumService.getConfidenTrend(tt, start_month, end_month);
					ret.setResult(Const.SUC, list);
				} else {
					List<MonthData> list = monthSumService.getMonthTrend(tt, start_month, end_month);
					ret.setResult(Const.SUC, list);
				}
			} else {
				ret.setResult(Const.FAIL, "没有此项指标");
			}

		} catch (Exception e) {
			ret.setResult(Const.FAIL, null);
			LOGGER.warn("查询get_month_trend异常：", e);
		}
		return ret;
	}

}
